//
//  YYLHomeRetWeetView.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/31.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import SnapKit

class YYLHomeRetWeetView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        pictureView.backgroundColor = self.backgroundColor
        
        contentLabel.snp.makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(homeCellMargin)
            make.trailing.equalTo(self).offset(-homeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(homeCellMargin)
            //暂时设定
            make.size.equalTo(CGSize(width: 100, height: 100))
            
        }

        //设置自身高度
        self.snp.makeConstraints { (make) in
            viewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
        }
    }

    //模型接收数据  设置子控件内容
    var originalVM: YYLOriginalViewModel?{
        didSet{
            let name = originalVM?.homeModel?.retweeted_status?.user?.screen_name
            contentLabel.text = "@\(name ?? ""):\(originalVM?.homeModel?.retweeted_status?.text ?? "")"
            
            //把绑定的约束清除掉
            viewBottomConstraint?.deactivate()
            
            //判断是否有图
            if let picUrls = originalVM?.homeModel?.retweeted_status?.pic_urls, picUrls.count > 0 {
                //有配图
                pictureView.picUrls = picUrls
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                  viewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
                })

            } else {
            //没有配图
                pictureView.isHidden = true
                self.snp.makeConstraints({ (make) in
                    viewBottomConstraint = make.bottom.equalTo(contentLabel).offset(homeCellMargin).constraint
                })
            }
            
            
        }
    }
    
    //懒加载控价
    lazy var contentLabel: UILabel = {
        let label = UILabel(text: "你发你大师傅靠你了的撒李开复你发你大师傅靠你了的撒李开复你发你大师傅靠你了的撒李开复你发你大师傅靠你了的撒李开复你发你大师傅靠你了的撒李开复", font: 13, color: UIColor.lightGray)
        label.numberOfLines = 0
        return label
    }()

    //懒加载控价 图片数组视图
    lazy var pictureView:YYLHomePictureView = YYLHomePictureView()
    
    //设置底部约束绑定
    fileprivate var viewBottomConstraint: Constraint?
    

}
